package com.jxc.jxc01.yk.web;

import com.alibaba.fastjson.JSONObject;
import com.jxc.jxc01.yk.entity.Customer;
import com.jxc.jxc01.yk.entity.PageHelper;
import com.jxc.jxc01.yk.entity.RespResult;
import com.jxc.jxc01.yk.entity.Sales;
import com.jxc.jxc01.yk.service.CustomerServiceImpl;
import com.jxc.jxc01.yk.service.ICustomerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("*.yk")
public class CustomerServlet extends HttpServlet {
    ICustomerService customerService = new CustomerServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        //获取请求地址
        String uri = request.getRequestURI();
        System.out.println("当前进入的uri地址==》" + uri);
        uri = uri.substring(uri.lastIndexOf("/") + 1, uri.lastIndexOf("."));

        if (uri.equals("listCustomer")) {
            System.out.println("进入到=》listCustomer");
            listCustomer(request, response);
        } else if (uri.equals("deleteOneOfCustomer")) {
            System.out.println("进入到=》deleteOneOfCustomer");
            deleteOneOfCustomer(request, response);
        } else if (uri.equals("updateOneOfCustomer")) {
            System.out.println("进入到=》updateOneOfCustomer");
            updateOneOfCustomer(request, response);
        } else if (uri.equals("addCustomer")) {
            System.out.println("进入到=》addCustomer");
            addCustomer(request, response);
        } else if (uri.equals("findCustomerNumber")) {
            System.out.println("进入到=》findCustomerNumber");
            findCustomerNumber(request, response);
        }
    }

    /**
     * 修改销售信息
     */
    private void addCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Customer customer = new Customer();
        //获取要增加的客户信息
        customer.setCustomername(request.getParameter("customername"));
        //String phoneStr = request.getParameter("phone");
        //if (phoneStr.equals(null) || phoneStr.equals("")){
        //    phoneStr = "0";
        //}
        //customer.setPhone(phoneStr);
        customer.setPhone(request.getParameter("phone"));
        customer.setAddress(request.getParameter("address"));
        customer.setMail(request.getParameter("mail"));
        String faxStr = request.getParameter("fax");
        if (faxStr.equals(null) || faxStr.equals("")){
            faxStr = "0";
        }
        customer.setFax(Integer.parseInt(faxStr));
        //customer.setFax(Integer.parseInt(request.getParameter("fax")));
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createcustomer_time = sdf.format(date);
        customer.setCreatecustomer_time(createcustomer_time);
        customer.setBank(request.getParameter("bank"));
        boolean addCustomer = customerService.addCustomer(customer);
        response.sendRedirect("kehu.html");
    }

    /**
     * 修改销售信息
     */
    private void updateOneOfCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Customer customer = new Customer();
        //获取要修改的信息
        customer.setId(Integer.parseInt(request.getParameter("id")));
        customer.setCustomername(request.getParameter("customername"));
        //String phoneStr = request.getParameter("phone");
        //if (phoneStr.equals(null) || phoneStr.equals("")){
        //    phoneStr = "0";
        //}
        //customer.setPhone(Integer.parseInt(phoneStr));
        customer.setPhone(request.getParameter("phone"));
        customer.setAddress(request.getParameter("address"));
        customer.setMail(request.getParameter("mail"));
        String faxStr = request.getParameter("fax");
        if (faxStr.equals(null) || faxStr.equals("")){
            faxStr = "0";
        }
        customer.setFax(Integer.parseInt(faxStr));
        //customer.setFax(Integer.parseInt(request.getParameter("fax")));
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createcustomer_time = sdf.format(date);
        customer.setCreatecustomer_time(createcustomer_time);
        customer.setBank(request.getParameter("bank"));
        boolean updateCustomer = customerService.updateCustomer(customer);
        if (!updateCustomer) {
            //修改失败
            return;
        }
        response.sendRedirect("kehu.html");
    }

    /**
     * 根据客户姓名查询客户信息
     */
    private void findCustomerNumber(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        List<Customer> customers = new ArrayList<>();
        //获取要查找的用户姓名
        //获取要查找的客户
        String customername = request.getParameter("customername");
        if (customername ==null || customername==""){
            customers = new ArrayList<>();
        }else {
            customers = customerService.findOneOfCustomer(customername);
        }
        RespResult result = new RespResult(0, "ok", customers.size(), customers);
        HttpSession session = request.getSession();
        session.setAttribute("findCustomerNumber", result);
        response.sendRedirect("kehu.html");
    }

    /**
     * 删除某一条销售记录
     */
    private void deleteOneOfCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Customer> customers = new ArrayList<>();
        RespResult result = null;
        int id = Integer.parseInt(request.getParameter("id"));
        //查询某一条销售记录
        boolean deleteCustomer = customerService.deleteCustomer(id);
        if (!deleteCustomer) {
            return;
        }
        response.sendRedirect("kehu.html");
    }

    /**
     * 查询所有销售清单
     */
    private void listCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        //查询session
        HttpSession session = request.getSession();
        RespResult findCustomerNumber = (RespResult) session.getAttribute("findCustomerNumber");
        if (findCustomerNumber == null) {
            List<Customer> customers = new ArrayList<>();
            PageHelper<Customer> pageHelper = new PageHelper<>();
            //查询销售清单
            customers = customerService.findCustomerList(pageHelper);
            System.out.println(customers.toString());
            RespResult result = new RespResult(0, "ok", customers.size(), customers);
            out.print(JSONObject.toJSONString(result));
        } else {
            out.print(JSONObject.toJSONString(findCustomerNumber));
            session.removeAttribute("findCustomerNumber");
        }
    }
}
